package handler

import (
	"github.com/gin-gonic/gin"
	"math/rand"
	"post-hub/inits"
	"post-hub/internal/model"
	"post-hub/pkg"
	"post-hub/response"
	"strconv"
	"time"
)

func SendSms(c *gin.Context) {

	var user model.User
	err := c.ShouldBind(&user)
	if err != nil {
		response.Error(c, "解析失败")
		return
	}
	code := rand.Intn(9000) + 1000
	sms, err := pkg.SendSms(user.Tel, strconv.Itoa(code))
	if err != nil {
		response.Error(c, "发送失败")
		return
	}
	if *sms.Body.Code != "OK" {
		response.Error(c, *sms.Body.Message)
	}
	response.Success(c, "发送成功", code)
	inits.Rdb.Set(inits.Ctx, "sendSms"+user.Tel, code, time.Minute*3)

}
